import 'package:canteen/class/dish.dart';
import 'package:canteen/class/set_meal.dart';
import 'package:canteen/class/ingredient.dart';

class Order {
  Order(
      {required this.dishes,
      required this.setMeals,
      this.pRoom = '',
      this.id = 0,this.name=''}) {
    _calcTI();
    _calcTD();
  }

   int id;
  String pRoom;
  Map<Dish, int> dishes = {};
  Map<SetMeal, int> setMeals = {};
  Map<Dish, int> totalDishes = {};
  Map<Ingredient, double> totalIngredients = {};
  String name;

  void _calcTI() {
    totalIngredients.clear();
    // 先计算 dishes 中的部分
    for (var dishEntry in dishes.entries) {
      for (var ingredientEntry in dishEntry.key.components.entries) {
        totalIngredients.putIfAbsent(ingredientEntry.key, () => 0.0);
        totalIngredients[ingredientEntry.key] =
            totalIngredients[ingredientEntry.key]! +
                ingredientEntry.value * dishEntry.value;
      }
    }
    // 再计算 setMeals 转化后的部分
    for (var setMealEntry in setMeals.entries) {
      for (var dishEntry in setMealEntry.key.dishes.entries) {
        for (var ingredientEntry in dishEntry.key.components.entries) {
          totalIngredients.putIfAbsent(ingredientEntry.key, () => 0.0);
          totalIngredients[ingredientEntry.key] =
              totalIngredients[ingredientEntry.key]! +
                  ingredientEntry.value * dishEntry.value * setMealEntry.value;
        }
      }
    }
  }

  void _calcTD() {
    totalDishes.clear();
    // 先加上 dishes 中的部分
    for (var dishEntry in dishes.entries) {
      totalDishes.putIfAbsent(dishEntry.key, () => 0);
      totalDishes[dishEntry.key] =
          totalDishes[dishEntry.key]! + dishEntry.value;
    }
    // 再加上 setMeals 转化后的部分
    for (var setMealEntry in setMeals.entries) {
      for (var dishEntry in setMealEntry.key.dishes.entries) {
        totalDishes.putIfAbsent(dishEntry.key, () => 0);
        totalDishes[dishEntry.key] =
            totalDishes[dishEntry.key]! + dishEntry.value * setMealEntry.value;
      }
    }
  }
}
